//
//  VMCBannerView.m
//  app
//
//  Created by bigknife on 14/11/15.
//  Copyright (c) 2014年 zzt. All rights reserved.
//

#import "VMCBannerView.h"

@interface VMCBannerView()<UIScrollViewDelegate, VMCBannerViewDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentHeight;
@property (nonatomic,weak) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (nonatomic,weak) IBOutlet UIScrollView *scroller;



@property (nonatomic,strong) NSTimer *timer;
@end

@implementation VMCBannerView

+(instancetype) newInstance {
    VMCBannerView  *inst = (VMCBannerView *)[UIView viewFromNib:@"banner_view"];
    return inst;
}


-(void)awakeFromNib {
    [super awakeFromNib];
	self.scroller.delegate = self;
}

-(void)setBannerData:(NSArray *)bannerData{
    _bannerData = bannerData;
    
    //add subview
    [self buildBannerImages];
	if ([bannerData count] != 0) {
		self.pageControl.numberOfPages = [bannerData count];
		self.pageControl.currentPageIndicatorTintColor = [UIColor darkGrayColor];
		self.pageControl.pageIndicatorTintColor = [UIColor whiteColor];
	}

    [self setNeedsLayout];
}

-(void)layoutSubviews{
	if (self.bannerData) {
		self.contentWidth.constant = [self.bannerData count] * CGRectGetWidth(self.frame);
		self.contentHeight.constant = 200;
	}
	
	[super layoutSubviews];

}

-(void) buildBannerImages {
    //remove all sub image views
    [[NSArray arrayWithArray:self.contentView.subviews] itWithBlock:^(UIView *item, NSInteger idx) {
        if ([item isKindOfClass:[UIImageView class]]) {
            [item removeFromSuperview];
        }
    }];
    
    __weak VMCBannerView *wself = self;
    CGFloat width = CGRectGetWidth(wself.frame);
    [self.bannerData itWithBlock:^(id item, NSInteger idx) {
		UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
		//UIViewContentModeTop
		button.contentMode = UIViewContentModeScaleAspectFit;
		button.clipsToBounds = YES;
		button.translatesAutoresizingMaskIntoConstraints = NO;
		[button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
		[wself.contentView addSubview:button];
		
        CGFloat leading = idx * width;
        NSArray *contraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-(leading)-[button(==width)]"
                                                                      options:0
                                                                      metrics:@{
                                                                                @"leading":[NSNumber numberWithFloat:leading],
                                                                                @"width" : [NSNumber numberWithFloat:width]
                                                                                }
                                                                        views:NSDictionaryOfVariableBindings(button)];
        [wself.contentView addConstraints:contraints];
		
        contraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button]|"
                                                             options:0
                                                             metrics:nil
                                                               views:NSDictionaryOfVariableBindings(button)];
        [wself.contentView addConstraints:contraints];
        [wself.contentView layoutIfNeeded];
        
//        [imageView sd_setImageWithURL:[NSURL URLWithString:[item objectForKey:@"imageUrl"]]];

		[button sd_setImageWithURL:[NSURL URLWithString:[item objectForKey:@"imageUrl"]] forState:UIControlStateNormal];
		
    }];
    
    //启动定时器
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2
                                                      target:self
                                                    selector:@selector(handleTick)
                                                    userInfo:nil
                                                     repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
}

-(void)btnAction:(id)sender
{
	[self.delegate play];
}

-(void)handleTick {
    
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
	NSInteger page = round(scrollView.contentOffset.x / scrollView.frame.size.width);
	self.pageControl.currentPage = page;
}
@end
